package com.aoxon.cargo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.service.RevampStoreService;
import com.aoxon.cargo.shared.SupIndexShared;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.DataUtil;
import com.aoxon.cargo.util.ToastUtil;

public class SupFillInfo1Activity extends BaseActivity implements
		NormalDataLoader.OnLoadDataListener {
	private String tag = "SupFillInfo1Activity";
	Supplier supplier;
	GsonBean gsonBean;

	ImageView ivReturn;
	TextView tvTitle, tvNext, tvJustNow ;
	EditText etShopName, etShopNum, etCompanyName, etFixPhone;
	private RevampStoreService revampStoreService = (RevampStoreService) MyServiceFactory
			.getInstance(MyServiceFactory.REVAMP_STORE);
	private NormalDataLoader normalDataLoader = new NormalDataLoader(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setup();
	}

	public void setup() {
		supplier = SupIndexShared.getSupplier();
		setContentView(R.layout.sup_fill_info1);
		ivReturn = (ImageView) findViewById(R.id.ivPublicTitleReturn);
		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);
		tvNext = (TextView) findViewById(R.id.tvNext_fill_info1);
		tvJustNow = (TextView) findViewById(R.id.tvJustNow_fill_info1);
		etShopName = (EditText) findViewById(R.id.etShopName_fill_info1);
		etShopNum = (EditText) findViewById(R.id.etShopNum_fill_info1);
		etCompanyName = (EditText) findViewById(R.id.etCompanyName_fill_info1);
		etFixPhone = (EditText) findViewById(R.id.etFixPhone_fill_info1);

		tvTitle.setText("完善信息-店铺");
		ivReturn.setOnClickListener(onClick);
		tvNext.setOnClickListener(onClick);
		tvJustNow.setOnClickListener(onClick);
		fillSupInfo(supplier);
		normalDataLoader.setOnloadDataListener(this);
		
		Log.v(tag, "Task Id:"+ this.getTaskId());
	}

	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.ivPublicTitleReturn:
				finish();
				break;
			case R.id.tvNext_fill_info1:
				updateSup();

				Intent intentFillInfo2 = new Intent(getBaseContext(),
						SupFillInfo2Activity.class);
				startActivity(intentFillInfo2);
//				SupFillInfo1Activity.this.finish();
				break;
			case R.id.tvJustNow_fill_info1:

				updateSup();
				CargoActivity.OPEN_CHOOSE = MyStateUtil.OPEN_MORE;
				Intent intentMore = new Intent(SupFillInfo1Activity.this,
						CargoActivity.class);
				startActivity(intentMore);
				finish();
				break;

			}
		}

	};

	// 添加已有数据
	private void fillSupInfo(Supplier supplier) {
		if (supplier != null && supplier.getStoreName() != null) {
			etShopName.setText(supplier.getStoreName());
		}
		if (supplier != null && supplier.getsupStall() != null) {
			etShopNum.setText(supplier.getsupStall());
		}
		if (supplier != null && supplier.getSupCompanyFullName() != null) {
			etCompanyName.setText(supplier.getSupCompanyFullName());
		}
	}

	private boolean updateSup() {

		boolean toUpdate = false;
		// supplier.setsupStall(etShopNum.getText().toString());
		// supplier.setSupCompanyFullName(etCompanyName.getText().toString());
		
		try{
			if (supplier != null) {
				if (!supplier.getStoreName()
						.equals(etShopName.getText().toString())) {
					supplier.setStoreName(etShopName.getText().toString());
					toUpdate = true;
				}
				if (!supplier.getsupStall().equals(etShopNum.getText().toString())) {
					supplier.setsupStall(etShopNum.getText().toString());
					toUpdate = true;
				}
				if (!supplier.getSupCompanyFullName().equals(
						etCompanyName.getText().toString())) {
					supplier.setSupCompanyFullName(etCompanyName.getText()
							.toString());
					toUpdate = true;
				}
		}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		// else{
		// supplier = new Supplier();
		// supplier.setStoreName(etShopName.getText().toString());
		// supplier.setsupStall(etShopNum.getText().toString());
		// supplier.setSupCompanyFullName(etCompanyName.getText()
		// .toString());
		// toUpdate = true;
		// }
		if (toUpdate) {
			normalDataLoader.load();
		}
		return toUpdate;
	}

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub
		ToastUtil.show(SupFillInfo1Activity.this, "正在更新数据");
		tvNext.setClickable(false);
		tvJustNow.setClickable(false);
	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub

		gsonBean = revampStoreService.execute(supplier);
		try {
			if (CheckStateUtil.check(gsonBean)) {
				SupIndexShared.saveSupplier(DataUtil.gson.toJson(supplier));
				msg.what = MyStateUtil.UPLOAD_RESULT;
			}
			tvNext.setClickable(true);
			tvJustNow.setClickable(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			msg.what=MyStateUtil.UPLOAD_ERROR;
			e.printStackTrace();
		}
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		switch (state) {
		case MyStateUtil.UPLOAD_RESULT:
			ToastUtil.show(getBaseContext(), "上传成功");
			break;
		case MyStateUtil.UPLOAD_ERROR:
			ToastUtil.show(getBaseContext(), "上传出错，请重新提交");
			break;
		case MyStateUtil.RETURN_FAIL:
			ToastUtil.show(getBaseContext(), "无对应数据");
			break;
		case MyStateUtil.NOT_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "无网络连接");
			break;
		case MyStateUtil.ERROR_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "网络连接异常");
			break;
		default:
			ToastUtil.show(getBaseContext(), "上传出错，请重新提交");
			break;
		
		}
		
	}
	
}
